using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Forum.Business
{
    public class MyThreadList : ForumUserControl,
        IReplyConverter<ThreadReply, GetListOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                requestBinders.Add(RequestHelper.BuildGetThreadListRequest(this, pager.PageSize, (int)ThreadOrderType.UpdateDate));
            }
        }

        private void BindData(ThreadReply reply)
        {
            if (list == null)
            {
                return;
            }

            list.DataSource = reply.EntityList;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(reply.TotalRecords);
                currentPage.TotalRecords = reply.TotalRecords;
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #region IReplyConverter<ThreadReply,GetListOperation> Members

        public void ConvertReply(ThreadReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                BindData(reply);
            }
        }

        #endregion
    }
}